<template>
  <el-dialog
    :visible.sync="dialogVisible"
    :title="title"
    :close-on-click-modal="false"
    width="800px"
    @close="close"
  >
    <el-form ref="form" v-loading="loading" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="所属门店：" prop="shopId">
        <el-select v-model="form.shopId" placeholder="请选择" :disabled="mode==='edit'&&business.syncTmpMerchantId">
          <el-option
            v-for="item in $store.state.user.manageShopListExcludeHeadquarters"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="打印机名称：" prop="name">
        <el-input v-model="form.name" placeholder="请输入名称" :disabled="mode==='edit'&&business.syncTmpMerchantId" />
      </el-form-item>

      <el-form-item label="品牌：" prop="brandType">
        <el-radio-group v-model="form.brandType" :disabled="mode==='edit'&&business.syncTmpMerchantId" @change="brandTypeChange">
          <el-radio
            v-for="(val,key,index) in brandOptions"
            :key="index"
            :label="Number(key)"
          >{{ val }}
          </el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item v-if="mode==='add'" label="打印纸尺寸：" prop="paperWidth">
        <el-radio-group v-model="form.paperWidth">
          <el-radio
            v-for="(val,index) in paperWidthOptions[form.brandType]"
            :key="index"
            :label="Number(val)"
          >{{ val + '毫米' }}
          </el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="SN号：" prop="snCode">
        <el-input v-model="form.snCode" placeholder="请输入SN号" :disabled="mode==='edit'&&business.syncTmpMerchantId" />
      </el-form-item>

      <el-form-item v-if="form.brandType!==3001&&mode==='add'" label="秘钥：" prop="snKey">
        <el-input v-model="form.snKey" placeholder="请输入秘钥" />
      </el-form-item>

      <el-form-item v-if="mode==='add'" label="备注：" prop="tag">
        <el-input v-model="form.tag" maxlength="30" placeholder="说明备注" />
      </el-form-item>

      <!--<div v-if="form.brandType===3001">-->
      <!--  <el-form-item label="打印的单据：" prop="type">-->
      <!--    <div v-if="$store.getters.features.includes('PAY')">-->
      <!--      <div>-->
      <!--        <span>收款：</span>-->
      <!--        <el-checkbox v-model="form.isPayResult">静态二维码收款凭证</el-checkbox>-->
      <!--        <el-checkbox v-model="form.isLitePay">简易扫码设备收款凭证</el-checkbox>-->
      <!--        <el-checkbox v-model="form.isLiteSettlement">简易扫码设备结算凭证</el-checkbox>-->
      <!--      </div>-->
      <!--    </div>-->
      <!--    <div v-if="$store.getters.features.includes('MALL')">-->
      <!--      <div>-->
      <!--        <span>商城：</span>-->
      <!--        <el-checkbox v-model="form.isMallDelivery">配送拣货单</el-checkbox>-->
      <!--        <el-checkbox v-model="form.isMallPicking">自提拣货单</el-checkbox>-->
      <!--      </div>-->
      <!--      <div>-->
      <!--        <span style="visibility: hidden">预售：</span>-->
      <!--        <el-checkbox v-model="form.isMallPresellDelivery">配送拣货单(预售)</el-checkbox>-->
      <!--        <el-checkbox v-model="form.isMallPresellPicking">自提拣货单(预售)</el-checkbox>-->
      <!--      </div>-->
      <!--    </div>-->
      <!--  </el-form-item>-->

      <!--</div>-->
      <div>
        <el-form-item label="打印的单据：" prop="type">
          <!--          <div v-if="$store.getters.features.includes('BOOKING')">-->
          <!--            <span>预订：</span>-->
          <!--            <el-checkbox v-model="form.isBookingConfirm">预订确认单</el-checkbox>-->
          <!--            <el-checkbox v-model="form.isBookingConsume">预订结算单</el-checkbox>-->
          <!--          </div>-->
          <div v-if="$store.getters.features.includes('WCANYIN')">
            <span>点餐：</span>
            <el-checkbox
              v-model="form.isCanyinProduce"
            >制作单<el-tooltip
              class="item"
              effect="dark"
              content="后厨制作单的小票"
              placement="top"
            >
              <i
                class="el-icon-question"
              /></el-tooltip>
              <el-button type="text" @click="openOrder(1)">查看样式</el-button></el-checkbox>
            <el-checkbox
              v-model="form.isCanyinConsume"
            >消费单<el-tooltip
              class="item"
              effect="dark"
              content="消费者已完成支付的堂食、打包订单小票"
              placement="top"
            >
              <i class="el-icon-question" /> </el-tooltip><el-button type="text" @click="openOrder(2)">查看样式</el-button></el-checkbox>
          </div>
          <!--          <div v-if="$store.getters.features.includes('DEPOSIT')">-->
          <!--            <span>寄存：</span>-->
          <!--            <el-checkbox v-model="form.isDepositCommit">寄存提交单</el-checkbox>-->
          <!--            <el-checkbox v-model="form.isDepositTake">寄存支取单</el-checkbox>-->
          <!--          </div>-->
          <!--          <div v-if="$store.getters.features.includes('PAY')">-->
          <!--            <div>-->
          <!--              <span>收款：</span>-->
          <!--              <el-checkbox v-model="form.isPayResult">静态二维码收款凭证</el-checkbox>-->
          <!--              <el-checkbox v-model="form.isLitePay">简易扫码设备收款凭证</el-checkbox>-->
          <!--              <el-checkbox v-model="form.isLiteSettlement">简易扫码设备结算凭证</el-checkbox>-->
          <!--            </div>-->
          <!--          </div>-->
          <!--          <div v-if="$store.getters.features.includes('COUPON')">-->
          <!--            <span>核券：</span>-->
          <!--            <el-checkbox v-model="form.isVerifyCoupon">核券凭证</el-checkbox>-->
          <!--          </div>-->
          <div v-if="$store.getters.features.includes('MALL')">
            <div>
              <span>商城：</span>
              <el-checkbox v-model="form.isMallDelivery">配送拣货单</el-checkbox>
              <el-checkbox v-model="form.isMallPicking">自提拣货单</el-checkbox>
            </div>
            <div>
              <span style="visibility: hidden">预售：</span>
              <el-checkbox v-model="form.isMallPresellDelivery">配送拣货单(预售)</el-checkbox>
              <el-checkbox v-model="form.isMallPresellPicking">自提拣货单(预售)</el-checkbox>
              <el-checkbox v-model="form.isExpress">快递单</el-checkbox>
            </div>
          </div>
          <div v-if="$store.getters.features.includes('APPOINTMENT')">
            <div>
              <span>预约：</span>
              <el-checkbox v-model="form.isAppointmentProject">预约项目订单</el-checkbox>
            </div>
          </div>
        </el-form-item>
      </div>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button size="small" round @click="hide">取 消</el-button>
      <el-button size="small" round type="primary" @click="handleSubmit('form')">确 定</el-button>
    </div>
    <DialogLookStyle ref="orderDialog" />

  </el-dialog>
</template>

<script>
import { validateStrLen } from '@/utils/validate.js'
import { editPrinter } from '@/api/printer'
import DialogLookStyle from './DialogLookStyle'
export default {
  components: {
    DialogLookStyle
  },
  props: {
    shopId: {
      require: true,
      type: Number
    },
    business: {
      require: true,
      type: Object,
      default: () => {}
    }
  },
  data() {
    // 名称
    const validateName = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入打印机名称'))
      } else if (!validateStrLen(value, 30)) {
        callback(new Error('长度不超过15个汉字或30个字符'))
      } else {
        callback()
      }
    }
    return {
      rules: {
        shopId: [
          {
            required: true,
            message: '请选择组织架构',
            trigger: ['change']
          }
        ],
        name: [
          {
            required: true,
            validator: validateName,
            trigger: ['blur', 'change']
          }
        ],
        brandType: [
          {
            required: true,
            message: '请选择品牌',
            trigger: ['change']
          }
        ],
        paperWidth: [
          {
            required: true,
            message: '请选择打印纸尺寸',
            trigger: ['change']
          }
        ],
        snCode: [
          {
            required: true,
            message: '请输入第三方SN号'
          }
        ],
        snKey: [
          {
            required: true,
            message: '请输入秘钥'
          }
        ],
        tag: [
          {
            message: '请输入',
            trigger: 'blur'
          }
        ]
      },
      // 配置
      brandOptions: {
        1001: '飞鹅',
        2001: '易联云',
        3001: '博实结'
      },
      paperWidthOptions: {
        1001: [58, 76, 80],
        2001: [58, 76, 80],
        3001: [58]
      },
      title: null,
      resolve: null,
      loading: false,
      reject: null,
      mode: null,
      dialogVisible: null,
      form: {
        status: 1,
        shopId: '',
        name: '',
        brandType: 1001,
        paperWidth: 58,
        snCode: '',
        snKey: '',
        tag: '',
        isBookingConfirm: false,
        isBookingConsume: false,
        isCanyinConsume: false,
        isCanyinProduce: false,
        isDepositCommit: false,
        isDepositTake: false,
        isPayResult: false,
        isLitePay: false,
        isLiteSettlement: false,
        isVerifyCoupon: false,
        isMallDelivery: false,
        isMallPicking: false,
        isMallPresellDelivery: false,
        isMallPresellPicking: false,
        isExpress: false,
        isAppointmentProject: false
      }
    }
  },
  methods: {
    openAdd() {
      this.mode = 'add'
      this.title = '新增打印机'
      this.form.shopId = this.shopId
      this.show()
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
      })
    },

    openEdit(row) {
      this.title = '编辑打印机'
      this.mode = 'edit'
      row.shopId = String(row.shopId)
      this.form = Object.assign({}, row)
      this.show()
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
      })
    },

    close() {
      this.form = {
        status: 1,
        shopId: '',
        name: '',
        brandType: 1001,
        paperWidth: 58,
        snCode: '',
        snKey: '',
        tag: '',
        isBookingConfirm: false,
        isBookingConsume: false,
        isCanyinConsume: false,
        isCanyinProduce: false,
        isDepositCommit: false,
        isDepositTake: false,
        isPayResult: false,
        isLitePay: false,
        isLiteSettlement: false,
        isVerifyCoupon: false,
        isMallDelivery: false,
        isMallPicking: false,
        isMallPresellDelivery: false,
        isMallPresellPicking: false,
        isExpress: false
      }
      this.loading = false
      this.$refs.form.resetFields()
    },

    show() {
      this.dialogVisible = true
    },

    hide() {
      this.resolve = null
      this.reject = null
      this.dialogVisible = false
    },

    handleSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // if (this.form.brandType === 3001) {
          //   this.form.isBookingConfirm = false
          //   this.form.isBookingConsume = false
          //   this.form.isCanyinConsume = false
          //   this.form.isCanyinProduce = false
          //   this.form.isDepositCommit = false
          //   this.form.isDepositTake = false
          //   this.form.isVerifyCoupon = false
          //   this.form.snKey = ''
          // }
          this.loading = true
          editPrinter(this.form)
            .then((res) => {
              this.$notify({
                title: '成功',
                message: this.mode === 'add' ? '新增成功' : '更新成功',
                type: 'success',
                duration: 2000,
                offset: 40
              })
              this.resolve && this.resolve()
              this.hide()
            })
            .catch(() => {
            })
            .finally(() => {
              this.loading = false
            })
        }
      })
    },

    /**
     * 状态
     */
    brandTypeChange(val) {
      if (val === 3001) {
        this.form.paperWidth = 58
      }
    },
    openOrder(type) {
      console.log(type)
      this.$refs.orderDialog.openDialog(type)
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.el-select {
  width: 100%;
}

.el-checkbox {
  width: 140px;
}
</style>
